﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLVT.Entity;
using QLVT.Config;
using System.Data.SqlClient;
using System.Data;
namespace QLVT.DAO
{
    class KhachHangDAO:DAOBase
    {
        public KhachHangDAO()
        {
            
        }

        public DataSet GetAll()
        {
            return Select("select * from KhachHang");
        }

        public KhachHang GetById(String makh)
        {
            KhachHang khachHang = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("select * from KhachHang where makh = '" + makh + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    khachHang = new KhachHang();
                    khachHang.Makh = dataReader.GetString(0);
                    khachHang.Holot = dataReader.GetString(1);
                    khachHang.Tenkh = dataReader.GetString(2);
                    khachHang.Sdt = dataReader.GetString(3);
                    khachHang.Ngaysinh = dataReader.GetDateTime(4);
                    khachHang.Gioitinh = dataReader.GetBoolean(5);
                    khachHang.Socmnd = dataReader.GetString(6);
                    return khachHang;
                }
            }
            catch (Exception ex)
            {

            }
            finally {
                conn.Close();
               
            }
            return khachHang;

        }

        public bool Insert(KhachHang khachHang)
        {
            String sql = "INSERT INTO [QLVT].[dbo].[khachhang]([manv],[holot],[tennv],[sdt],[ngaysinh],[gioitinh],[socmnd])"
               + "VALUES('" + khachHang.Makh + "','" + khachHang.Holot + "','" + khachHang.Tenkh + "','" + khachHang.Sdt + "','" + khachHang.Ngaysinh.ToShortDateString() + "','" + khachHang.Gioitinh + "','" + khachHang.Socmnd + "')";
            return Update(sql);
        }

        public bool DeleteById(String makh)
        {
            return Update("delete from KhachHang where makh = '" + makh + "'");
        }

        public bool UpdateById(KhachHang khachHang)
        {
            String sql = "update KhachHang "
                 + "set holot = '" + khachHang.Holot.Trim() + "'"
                 + ", tenkh = '" + khachHang.Tenkh.Trim() + "'"
                 + ", sdt = '" + khachHang.Sdt.Trim() + "'"
                 + ", ngaysinh = '" + khachHang.Ngaysinh.ToShortDateString() + "'"
                 + ", gioitinh = '" + khachHang.Gioitinh + "'"
                 + ", socmnd = '" + khachHang.Socmnd.Trim() + "'"
                 + " where makh = '" + khachHang.Makh.Trim() + "'";
            return Update(sql);
        }
    }
}
